const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

const defaultConfigs = [
  {
    chain: 'ethereum',
    isEnabled: true,
    rpcUrl: 'https://ethereum.publicnode.com',
    wssUrl: 'wss://ethereum.publicnode.com',
    minValueUsd: 5000.00,
    blockTime: 12000,
    maxRetries: 3,
    requestDelay: 1000,
    batchSize: 100,
    fallbackRpcs: [
      'https://ethereum.publicnode.com',
      'https://eth.llamarpc.com',
      'https://rpc.ankr.com/eth',
      'https://ethereum-rpc.publicnode.com'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  {
    chain: 'bsc',
    isEnabled: true,
    rpcUrl: 'https://bsc-dataseed.binance.org/',
    wssUrl: null,
    minValueUsd: 5000.00,
    blockTime: 3000,
    maxRetries: 3,
    requestDelay: 1000,
    batchSize: 100,
    fallbackRpcs: [
      'https://bsc-dataseed.binance.org/',
      'https://bsc-dataseed1.binance.org/',
      'https://bsc-dataseed2.binance.org/',
      'https://bsc-dataseed3.binance.org/'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  {
    chain: 'bitcoin',
    isEnabled: true,
    rpcUrl: 'https://blockstream.info/api',
    wssUrl: null,
    minValueUsd: 10000.00,
    blockTime: 600000,
    maxRetries: 3,
    requestDelay: 2000,
    batchSize: 50,
    fallbackRpcs: [
      'https://blockstream.info/api',
      'https://bitcoin.publicnode.com',
      'https://api.blockcypher.com/v1/btc/main'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  {
    chain: 'base',
    isEnabled: false,
    rpcUrl: 'https://base.publicnode.com',
    wssUrl: 'wss://base.publicnode.com',
    minValueUsd: 2000.00,
    blockTime: 2000,
    maxRetries: 3,
    requestDelay: 1000,
    batchSize: 100,
    fallbackRpcs: [
      'https://base.publicnode.com',
      'https://mainnet.base.org',
      'https://base-rpc.publicnode.com'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  {
    chain: 'polygon',
    isEnabled: false,
    rpcUrl: 'https://polygon-rpc.com/',
    wssUrl: 'wss://polygon-bor.publicnode.com',
    minValueUsd: 2000.00,
    blockTime: 2000,
    maxRetries: 3,
    requestDelay: 1000,
    batchSize: 100,
    fallbackRpcs: [
      'https://polygon-rpc.com/',
      'https://rpc.ankr.com/polygon',
      'https://polygon.publicnode.com'
    ],
    errorCount: 0,
    totalTxCount: 0
  },
  {
    chain: 'solana',
    isEnabled: false,
    rpcUrl: 'https://api.mainnet-beta.solana.com',
    wssUrl: 'wss://api.mainnet-beta.solana.com',
    minValueUsd: 5000.00,
    blockTime: 400,
    maxRetries: 3,
    requestDelay: 500,
    batchSize: 50,
    fallbackRpcs: [
      'https://api.mainnet-beta.solana.com',
      'https://solana.publicnode.com',
      'https://rpc.ankr.com/solana'
    ],
    errorCount: 0,
    totalTxCount: 0
  }
];

async function initChainConfigs() {
  try {
    console.log('🔧 初始化链监控配置...');

    for (const config of defaultConfigs) {
      await prisma.chainMonitorConfig.upsert({
        where: { chain: config.chain },
        update: {
          rpcUrl: config.rpcUrl,
          wssUrl: config.wssUrl,
          fallbackRpcs: config.fallbackRpcs,
          updatedAt: new Date()
        },
        create: config
      });
      console.log(`✅ ${config.chain} 配置已初始化`);
    }

    console.log('🎉 所有链监控配置初始化完成！');
    
    // 显示当前配置
    const configs = await prisma.chainMonitorConfig.findMany({
      orderBy: { chain: 'asc' }
    });
    
    console.log('\n📋 当前配置:');
    configs.forEach(config => {
      console.log(`${config.chain}:`);
      console.log(`  - 启用: ${config.isEnabled}`);
      console.log(`  - RPC: ${config.rpcUrl}`);
      console.log(`  - 最小金额: $${config.minValueUsd}`);
      console.log(`  - 区块时间: ${config.blockTime}ms`);
    });
    
  } catch (error) {
    console.error('❌ 初始化失败:', error);
  } finally {
    await prisma.$disconnect();
  }
}

initChainConfigs(); 